<%--

This file is part of Annuaire.
 
Annuaire is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Annuaire is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Annuaire.  If not, see <http://www.gnu.org/licenses/>.

    Document   : visualisationPDF
    Created on : 25 mai 2011, 12:04:18
    Author     : comeulie
--%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="org.miage.annuaire.pdf.xml2pdf"%>
<%@page import="org.w3c.dom.Document"%>
<%@page import="org.miage.annuaire.data.XmlDataAccessImpl"%>
<%
    InputStream xsl = getServletContext().getResourceAsStream("WEB-INF/configurationXML/pdf.xsl");
    //InputStream outf = getServletContext().getResourceAsStream("\\web\\WEB-INF\\configurationXML\\out\\Annuaire.pdf");


    // il faudra recherhc e les structure
    /*Object o = null;
    Enumeration name = request.getParameterNames();
    while(  name.hasMoreElements() ) {
        o = name.nextElement();
        out.print( o.toString());
    }

    
    //ObjectDataAccessImpl data = new ObjectDataAccessImpl();
*/
    // debug
    XmlDataAccessImpl data = new XmlDataAccessImpl();
    Document doc = data.getMembresByName("TOTO");

    xml2pdf pdf = new xml2pdf();
    //FileInputStream f = pdf.extract2stream(doc);
    FileInputStream f = pdf.extract2stream(doc,xsl,getServletContext().getRealPath(""));

    ServletOutputStream stream = response.getOutputStream();

      //set response headers
      response.setContentType("application/pdf");
      response.addHeader("Content-Disposition", "attachment; filename=Annuaire.pdf");

      //URLConnection urlConn = pdfDir.openConnection();
      //response.setContentLength((int) urlConn.getContentLength());

      BufferedInputStream buf = new BufferedInputStream(f);//urlConn.getInputStream());
      int readBytes = 0;

      //read from the file; write to the ServletOutputStream
      //try {
        while ( (readBytes = buf.read()) != -1)
            stream.write(readBytes);

      //} catch (IOException ioe) {
            //throw new ServletException(ioe.getMessage());
      //} finally {
        if (stream != null)  stream.close();
            buf.close();
      //}

   %>